package com.general.client.widgets.button.tab;

import com.general.client.action.ActionHandler;
import com.general.client.basewidget.BaseWidget;
import com.general.client.bundle.CommonBundle;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class TabButton extends BaseWidget implements ITabButton {

	@UiField
	Image icon = new Image();
	@UiField
	Label title = new Label();
	@UiField
	Label subtitle = new Label();

	@UiField
	FocusPanel mainPanel;

	@UiField
	CommonBundle common;

	@UiTemplate("TabButton.ui.xml")
	interface TabButtonUiBinder extends UiBinder<Widget, TabButton> {
	}

	public static TabButtonUiBinder uiBinder = GWT
			.create(TabButtonUiBinder.class);

	protected UiBinder<Widget, TabButton> getUiBinder() {
		return uiBinder;
	}

	@Override
	public void setIconUrl(String url) {
		icon.setUrl(url);
	}

	@Override
	public void setTabTitle(String title) {
		this.title.setText(title);
	}

	@Override
	public void setTabSubtitle(String subtitle) {
		this.subtitle.setText(subtitle);
	}

	public TabButton() {
		init();
		mainPanel.addFocusHandler(new FocusHandler() {
			@Override
			public void onFocus(FocusEvent event) {
				mainPanel.addStyleName(common.style().focused());
			}
		});
		mainPanel.addBlurHandler(new BlurHandler() {
			@Override
			public void onBlur(BlurEvent event) {
				mainPanel.removeStyleName(common.style().focused());
			}
		});
	}

	@Override
	public void setSelected(boolean selected) {
		if (selected)
			mainPanel.removeStyleName(common.style().inactive());
		else
			mainPanel.addStyleName(common.style().inactive());
		// this.selected = selected;
	}

	protected void init() {
		initWidget(getUiBinder().createAndBindUi(this));
		setSelected(false);
		mainPanel.addMouseDownHandler(new MouseDownHandler() {

			@Override
			public void onMouseDown(MouseDownEvent event) {
				setSelected(true);
			}
		});
		mainPanel.addKeyDownHandler(new KeyDownHandler() {

			@Override
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					setSelected(true);
				}
			}
		});
	}

	@Override
	public HandlerRegistration addActionHandler(final ActionHandler handler) {

		final HandlerRegistration clickHandler = mainPanel
				.addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						handler.onAction(event);
					}
				});

		final HandlerRegistration keyHandler = mainPanel
				.addKeyDownHandler(new KeyDownHandler() {

					@Override
					public void onKeyDown(KeyDownEvent event) {
						if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
							handler.onAction(event);
						}
					}
				});

		return new HandlerRegistration() {

			@Override
			public void removeHandler() {
				keyHandler.removeHandler();
				clickHandler.removeHandler();
			}
		};

	}

	@Override
	public Widget asWidget() {
		return mainPanel;
	}

}
